package com.xindacloud.sms.console.result;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;

/**
 * 响应结果生成工具
 */
public class ResultGenerator {
	private static final String	DEFAULT_SUCCESS_MESSAGE			= "OK";
	private static final String	DEFAULT_FAIL_MESSAGE			= "SERVER ERROR";
	private static final String	DEFAULT_PARTIAL_SUCCESS_MESSAGE	= "PARTIAL SUCCESS, SERVER SIDE";

	public static Result genSuccessResult() {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(DEFAULT_SUCCESS_MESSAGE));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.SUCCESS.code);
		resultBody.setMessage(DEFAULT_SUCCESS_MESSAGE);

		Result result = new Result(resultBody, headers, HttpStatus.OK);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genSuccessResult(Object response) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(DEFAULT_SUCCESS_MESSAGE));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.SUCCESS.code);
		resultBody.setMessage(DEFAULT_SUCCESS_MESSAGE);
		resultBody.setObject(response);

		Result result = new Result(resultBody, headers, HttpStatus.OK);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genPartialSuccessResult() {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(DEFAULT_PARTIAL_SUCCESS_MESSAGE));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.PARTIAL_SUCCESS.code);
		resultBody.setMessage(DEFAULT_PARTIAL_SUCCESS_MESSAGE);

		Result result = new Result(resultBody, headers, HttpStatus.PARTIAL_CONTENT);
		result.setResultBody(resultBody);
		return result;

	}

	public static Result genPartialSuccessResult(Object data) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(DEFAULT_PARTIAL_SUCCESS_MESSAGE));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.PARTIAL_SUCCESS.code);
		resultBody.setMessage(DEFAULT_PARTIAL_SUCCESS_MESSAGE);
		resultBody.setObject(data);

		Result result = new Result(resultBody, headers, HttpStatus.PARTIAL_CONTENT);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genClientFailResult(String message) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(message));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.FAIL_CLIENT.code);
		resultBody.setMessage(message);

		Result result = new Result(resultBody, headers, HttpStatus.BAD_REQUEST);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genClientFailResult(HttpStatus httpStatus, String message) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(message));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(httpStatus.value());
		resultBody.setMessage(message);

		Result result = new Result(resultBody, headers, httpStatus);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genServerFailResult(String message) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(message));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.FAIL_SERVER.code);
		resultBody.setMessage(message);

		Result result = new Result(resultBody, headers, HttpStatus.INTERNAL_SERVER_ERROR);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genServerFailResult(Object data) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(DEFAULT_FAIL_MESSAGE));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.FAIL_SERVER.code);
		resultBody.setMessage(DEFAULT_FAIL_MESSAGE);
		resultBody.setObject(data);

		Result result = new Result(resultBody, headers, HttpStatus.INTERNAL_SERVER_ERROR);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genServerFailResult(Exception exception) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(exception.getMessage()));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.FAIL_SERVER.code);
		resultBody.setMessage(exception.getMessage());
		resultBody.setException(exception);

		Result result = new Result(resultBody, HttpStatus.INTERNAL_SERVER_ERROR);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genServerFailResult() {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(DEFAULT_FAIL_MESSAGE));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.FAIL_SERVER.code);
		resultBody.setMessage(DEFAULT_FAIL_MESSAGE);

		Result result = new Result(resultBody, headers, HttpStatus.INTERNAL_SERVER_ERROR);
		result.setResultBody(resultBody);
		return result;
	}

	public static Result genClientFailResult(BindingResult bindingResult) {
		String message = "error";
		try {
			message = bindingResult.getAllErrors().get(0).getDefaultMessage();
		} catch (Exception e) {

		}
		HttpHeaders headers = new HttpHeaders();
		headers.add("Access-Control-Expose-Headers", "message");
		headers.add("message", encodeMessage(message));

		ResultBody resultBody = new ResultBody();
		resultBody.setCode(ResultCode.FAIL_CLIENT.code);
		resultBody.setMessage(message);

		Result result = new Result(resultBody, headers, HttpStatus.BAD_REQUEST);
		result.setResultBody(resultBody);
		return result;
	}

	// 编码message
	private static String encodeMessage(String message) {
		try {
			return URLEncoder.encode(message, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return message;
		}
	}

}
